#include "_SetPhonebookpageCallback.h"
#include <QString>
#include "AnGlobal.h"
#include "AnAdbServer.h"
#include <QJsonDocument>
#include <iostream>
#include <QWidget>
#include <QFileDialog>
#include <QMessageBox>


_SetPhonebookpageCallback::_SetPhonebookpageCallback()
{

}

_SetPhonebookpageCallback::_SetPhonebookpageCallback(AnMainwin * anMainwin)
{
    amw=anMainwin;
    movie=new QMovie(":/img/loading.gif");
}

_SetPhonebookpageCallback::~_SetPhonebookpageCallback()
{

}

void _SetPhonebookpageCallback::response(QJsonObject an_resp)
{
    std::cout << "response: \n" << QJsonDocument(an_resp).toJson().data() << std::endl;
    if(!QString::compare(an_resp.value(NAME).toString(),"contacts_backup",Qt::CaseSensitive))
    {
        if(an_resp.value(DATA).toObject().value(RESULT).toInt())
        {
            firstDialog->close();
            firstMessageBox=new QMessageBox(amw);
            firstMessageBox->setIconPixmap(QPixmap(":/img/sign-check.png"));
            firstMessageBox->setText("导出成功");
            firstMessageBox->show();
        }
        else
        {
            firstMessageBox=new QMessageBox(amw);
            firstMessageBox->setIcon(QMessageBox::Icon::Critical);
            firstMessageBox->setText("导出失败");
            firstMessageBox->show();
        }
    }
    if(!QString::compare(an_resp.value(NAME).toString(),"contacts_recovery",Qt::CaseSensitive))
    {
        if(an_resp.value(DATA).toObject().value(RESULT).toInt())
        {
            firstDialog->close();
            delete firstDialog;
            firstMessageBox=new QMessageBox(amw);
            firstMessageBox->setIconPixmap(QPixmap(":/img/sign-check.png"));
            firstMessageBox->setText("导入成功");
            firstMessageBox->show();
        }
        else
        {
            firstDialog->close();
            delete firstDialog;
            firstMessageBox=new QMessageBox(amw);
            firstMessageBox->setIcon(QMessageBox::Icon::Critical);
            firstMessageBox->setText("导入失败");
            firstMessageBox->show();
        }
    }
}

void _SetPhonebookpageCallback::backup_clicked()
{
    if(AnGlobal::is_connecting())
    {
        QString filePath=QFileDialog::getSaveFileName(amw, tr("保存"), "/home/contact.bak",tr("*.bak"));
        if(filePath==NULL)
        {
            return;
        }

        QJsonObject reqs;
        reqs.insert(NAME,QJsonValue("contacts_backup"));
        reqs.insert(STATUS,QJsonValue(REQUEST));
        QJsonObject data;
        data.insert(PATH,QJsonValue(filePath));
        reqs.insert(DATA,data);

        adb_server->adbServerManager(reqs,this);
        std::cout << "request: \n" << QJsonDocument(reqs).toJson().data() << std::endl;
        firstDialog=new QDialog(amw);
        firstDialog->resize(300,100);
        firstDialog->setWindowFlags(Qt::FramelessWindowHint);
        firstDialog->setModal(true);
        firstDialog->move((amw->width()-firstDialog->width())/2,(amw->height()-firstDialog->height())/2);
        QLabel *movLabel=new QLabel(firstDialog);
        QLabel *diaLabel=new QLabel(firstDialog);
        diaLabel->setText("正在导出，请稍候...");
        diaLabel->setGeometry(100,25,150,50);
        movLabel->setMovie(movie);
        movLabel->setGeometry(50,25,50,50);
        firstDialog->open();
        movie->start();
    }
}

void _SetPhonebookpageCallback::recover_clicked()
{
    if(AnGlobal::is_connecting())
    {
        QString filePath=QFileDialog::getOpenFileName(amw, tr("打开"), "/home");
        if(filePath==NULL)
        {
            return;
        }

        QJsonObject reqs;
        reqs.insert(NAME,QJsonValue("contacts_recovery"));
        reqs.insert(STATUS,QJsonValue(REQUEST));
        QJsonObject data;
        data.insert(PATH,QJsonValue(filePath));
        reqs.insert(DATA,data);
        adb_server->adbServerManager(reqs,this);
        std::cout << "request: \n" << QJsonDocument(reqs).toJson().data() << std::endl;
        firstDialog=new QDialog(amw);
        firstDialog->resize(300,100);
        firstDialog->setWindowFlags(Qt::FramelessWindowHint);
        firstDialog->setModal(true);
        firstDialog->move((amw->width()-firstDialog->width())/2,(amw->height()-firstDialog->height())/2);
        QLabel *movLabel=new QLabel(firstDialog);
        QLabel *diaLabel=new QLabel(firstDialog);
        diaLabel->setText("正在导入，请稍候...");
        diaLabel->setGeometry(100,25,150,50);
        movLabel->setMovie(movie);
        movLabel->setGeometry(50,25,50,50);
        firstDialog->open();
        movie->start();
    }
}
